package com.gitee.wsl.common.ui.draw.node

import androidx.annotation.FloatRange
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.DrawScope.Companion.DefaultBlendMode
import androidx.compose.ui.graphics.drawscope.DrawStyle
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.base.ColorOrBrush
import com.gitee.wsl.common.ui.base.Point
import com.gitee.wsl.common.ui.draw.api.HasFill
import com.gitee.wsl.common.ui.draw.api.HasStroke
import com.gitee.wsl.common.ui.draw.container.GroupNode
import com.gitee.wsl.common.ui.draw.style.toStroke


class RectNode(
    val node: Rect
) : Node(), HasFill, HasStroke {

    override fun DrawScope.draw() {
        fill?.let {
            render(it, Fill)
        }
        strokeColor?.let {colorOrGradient ->
            strokeWidth?.let {
                val stroke = strokeWidth?.toStroke(this) ?: Stroke()
                render(colorOrGradient, stroke)
            }
        }
    }

        private fun DrawScope.render(colorOrBrush: ColorOrBrush,
                                     style: DrawStyle
        ) {
            val topLeft = Offset(node.left.dp.toPx(), node.top.dp.toPx())
            val s = Size(node.width.dp.toPx(), node.height.dp.toPx())
            drawRect(
                colorOrBrush = colorOrBrush,
                topLeft,
                size = s,
                style = style
            )

            /*when(colorOrBrush){
                is ColorOrBrush.Color ->
                    drawRect(
                        color = colorOrBrush.value,
                        topLeft,
                        size=s,
                        style = style
                    )
                is ColorOrBrush.Brush ->
                    drawRect(
                        brush = colorOrBrush.value,
                        topLeft,
                        size=s,
                        style = style
                    )
        }*/
    }

}

fun GroupNode.rect(block: RectNode.(GroupNode)->Unit){
    val node=RectNode(Rect(Point(),Point()))
    node.block(this)
    add(node)
}

fun DrawScope.drawRect(
    colorOrBrush: ColorOrBrush,
    topLeft: Offset = Offset.Zero,
    size: Size = this.size.offsetSize(topLeft),
    @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DefaultBlendMode
){
    when(colorOrBrush){
        is ColorOrBrush.Color ->
            drawRect(
                color = colorOrBrush.value,
                topLeft,
                size=size,
                style = style,
                colorFilter = colorFilter,
                blendMode = blendMode
            )
        is ColorOrBrush.Brush ->
            drawRect(
                brush = colorOrBrush.value,
                topLeft,
                size=size,
                style = style,
                colorFilter = colorFilter,
                blendMode = blendMode
            )
    }
}

fun Size.offsetSize(offset: Offset): Size =
    Size(this.width - offset.x, this.height - offset.y)